package study.stack;

public class ArrayStack {
    private int maxSize;
    private int top = -1;
    private int[] stack;

    ArrayStack(int maxSize){
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }

    //桟满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    //桟空
    public boolean isEmpty(){
        return top == -1;
    }

    //入栈
    public void push(int data){
        if (isFull()){
            System.out.println("桟满，无法入栈！");
            return;
        }
        top++;
        stack[top] = data;

    }

    //出栈
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("桟空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历
    public void print(){
        for (int i = top; i >= 0 ; i--) {
            System.out.printf("stack[%d] = %d\n", i, stack[i]);
        }
    }
}
